<template>
  <div class="header">

    <div :class="ifFixed == true ? isShowtop : ''">
      <div class="box">
        <span class="color">
          <!-- 温岭百科 -->
        </span>
        <el-menu :default-active="activeIndex" router class="el-menu-demo" mode="horizontal" @select="handleSelect"
          background-color="#00000000" text-color="#000" active-text-color="#000">
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/foods"> 美景美食</el-menu-item>
          <el-menu-item index="/folkloric">民风民俗</el-menu-item>
          <el-menu-item index="/news">时事新闻</el-menu-item>
        </el-menu>
        <span class="login">
          <router-link :to="{path:'/login'}" replace>后台登录</router-link>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        activeIndex: '/',
        ifFixed: false,
        isShowtop: ["animate__animated", "animate__bounce", "clickClass", "isFixed"],
      }
    },
    created() {
      this.setCurrentRoute();
      window.addEventListener("scroll", this.handleScroll);
    },
    beforeDestroy() {
      window.removeEventListener("scroll", this.handleScroll);
    },
    watch: {
      $route() {
        this.setCurrentRoute();
      }
    },
    methods: {
      handleSelect(key, keyPath) {
        // console.log(key, keyPath);
      },
      setCurrentRoute() {
        this.activeIndex = this.$route.path; // 通过他就可以监听到当前路由状态并激活当前菜单
      },
      handleScroll() {

        let top = document.documentElement.scrollTop
        // console.log(top)
        if (top > 60) {
          this.ifFixed = true
          // console.log('111')
        } else {
          this.ifFixed = false
        }
      }
    }
  }

</script>

<style lang="scss" scoped>
  .header {
    width: 100%;
    z-index: 999;
    .box {
      // background-color: rgba(39, 255, 0, 0.1);
      background-color: rgb(255, 255, 255);
      box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
      // background-color: transparent;
    }
    // background-color: rgba(0, 0, 0, 0.445);
    .el-menu {
      border: 0;
    }
    .el-menu-demo {
      // padding-left: 300px;
      display: flex;
      justify-content: center;
    }
    .el-menu-item {
      width: 200px;
      text-align: center;
    }
    .el-menu-item.is-active {
      background-color: rgba(1, 149, 51, .5) !important;
    }
    .el-menu-item:hover {
      background-color: rgba(1, 149, 51, .2) !important;
    }
    .color {
      // font-size: 36px;
      position: absolute;
      top: -15px;
      // left: 50px;
      // color: rgba(255,255,255,.8);
      width: 400px;
      height: 100px;
      background: url('../assets/font.png');
      background-size: 100% 100%;
    }
    .login {
      // margin-left: 950px;
      position: absolute;
      top: 20px;
      right: 50px;
    }
    a {
      text-decoration: none;
      color: #000;
    }
    .router-link-active {
      text-decoration: none;
      color: red;
    }
  }
  .isFixed {
    width: 100%;
    position: fixed;
    top: 0;
    z-index: 999;
  }
</style>
